#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName  :垂类榜单.py
# @Time      :2024/11/26 
# @Author    :CL
# @email     :1037654919@qq.com
# 垂类榜单 参数解释：开始日期，结束日期，目录词典；
from scrapy_juliangsuanshu import *


def content_creative_keywords_run(Begin_date, End_date, contentdict):
    print('begin function:content_creative_keywords')
    datestart = datetime.datetime.strptime(Begin_date, '%Y%m%d')
    dateend = datetime.datetime.strptime(End_date, '%Y%m%d')
    ii = 0  #进度条
    for item, tagid in contentdict.items():  #根据热词大类循环
        ii += 1
        print('进度：%d/%d' % (ii, len(contentdict)), item, tagid, end='\n')
        for dt in tqdm(rrule(DAILY, dtstart=datestart, until=dateend)):  #根据日期循环
            date = dt.strftime('%Y%m%d')
            dataz = pd.DataFrame()
            time.sleep(0.1)
            try:
                data = content_creative_keywords(tagid, date)
                # print(type(data),data)
                data1 = data['keyword_list']
                # print(type(data1),data1)
                for value in data1:
                    dataz = dataz.append(
                        [{'tag_id': tagid, 'item': item, 'keyword': value['keyword'], 'date': date, 'cnt': value['cnt'],
                          'is_hot': value['is_hot'], 'index': value['index'],
                          'search_index': value['search_index']}],
                        ignore_index=True)
                yield dataz
            except:
                print('%s %s fialed' % (item, date))
        if ii == len(contentdict):
            print('complete:%s大类,and a good news:completed all' % (item))
        else:
            print('complete:%s大类,actually we should take a rest:60s' % (item))
            time.sleep(60)
    return 1


if __name__ == '__main__':
    content_creative_keywords_run('20220301', '20220307', contentdict, browser)
